{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Azure DevOps\n",
    "Python API samples\n",
    "\n",
    "Click \"Run\" in the Jupyter menu to get started.\n",
    "\n",
    "                    vSTs\n",
    "                  vSTSVSTSv\n",
    "                vSTSVSTSVST\n",
    " VSTS         vSTSVSTSVSTSV\n",
    " VSTSVS     vSTSVSTSV STSVS\n",
    " VSTSVSTSvsTSVSTSVS   TSVST\n",
    " VS  tSVSTSVSTSv      STSVS\n",
    " VS   tSVSTSVST       SVSTS\n",
    " VS tSVSTSVSTSVSts    VSTSV\n",
    " VSTSVST    SVSTSVSTs VSTSV\n",
    " VSTSv        STSVSTSVSTSVS\n",
    "                VSTSVSTSVST\n",
    "                  VSTSVSTs\n",
    "                    VSTs    (TM)\n",
    "\"\"\"\n",
    "\n",
    "import sys\n",
    "# tell python to look in .\\src for loading modules\n",
    "sys.path.insert(1, 'src')\n",
    "\n",
    "from IPython.display import display\n",
    "import ipywidgets as widgets\n",
    "\n",
    "import runner\n",
    "import runner_lib\n",
    "\n",
    "\n",
    "def build_areas_and_resources():\n",
    "    value = {\n",
    "        'all': ['all',],\n",
    "    }\n",
    "    \n",
    "    for area in runner_lib.discovered_samples.keys():\n",
    "        value[area] = ['all',]\n",
    "        value[area].extend(runner_lib.discovered_samples[area].keys())\n",
    "    \n",
    "    return value\n",
    "\n",
    "areas_and_resources = build_areas_and_resources()\n",
    "\n",
    "\n",
    "# build and display widgets\n",
    "url_widget = widgets.Text(\n",
    "    value='',\n",
    "    placeholder='https://fabrikam.visualstudio.com',\n",
    "    description='Account URL'\n",
    ")\n",
    "pat_widget = widgets.Text(\n",
    "    value='',\n",
    "    placeholder='VSTS PAT',\n",
    "    description='PAT'\n",
    ")\n",
    "area_widget = widgets.ToggleButtons(\n",
    "    options=areas_and_resources.keys(),\n",
    "    description='Area'\n",
    ")\n",
    "resource_widget = widgets.ToggleButtons(\n",
    "    options=areas_and_resources[area_widget.value],\n",
    "    description='Resource'\n",
    ")\n",
    "run_widget = widgets.Button(\n",
    "    description='Run samples',\n",
    "    button_style='success',\n",
    "    icon='play'\n",
    ")\n",
    "\n",
    "def on_area_change(change):\n",
    "    resource_widget.options = areas_and_resources[change['new']]\n",
    "\n",
    "area_widget.observe(on_area_change, names='value')\n",
    "\n",
    "def on_run_click(b):\n",
    "    if not url_widget.value or not pat_widget.value:\n",
    "        print('You must specify a URL and PAT to run these samples.')\n",
    "        return\n",
    "    \n",
    "    print('running samples...')\n",
    "    print('-------------------------')\n",
    "    print()\n",
    "    runner.main(url=url_widget.value,\n",
    "                auth_token=pat_widget.value,\n",
    "                area=area_widget.value,\n",
    "                resource=resource_widget.value\n",
    "    )\n",
    "    print()\n",
    "    print('-------------------------')\n",
    "    print('done!')\n",
    "\n",
    "run_widget.on_click(on_run_click)\n",
    "\n",
    "display(url_widget)\n",
    "display(pat_widget)\n",
    "display(area_widget)\n",
    "display(resource_widget)\n",
    "display(run_widget)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
